<?php

/**
 * +----------------------------------------------------------------------
 * | 用户中心接口
 * | 需要验证主页用户登录信息
 * |
 * +----------------------------------------------------------------------
 *                                            ...     .............
 *                                          ..   .:!o&*&&&&&ooooo&; .
 *                                        ..  .!*%*o!;.
 *                                      ..  !*%*!.      ...
 *                                     .  ;$$!.   .....
 *                          ........... .*#&   ...
 *                                     :$$: ...
 *                          .;;;;;;;:::#%      ...
 *                        . *@ooooo&&&#@***&&;.   .
 *                        . *@       .@%.::;&%$*!. . .
 *          ................!@;......$@:      :@@$.
 *                          .@!   ..!@&.:::::::*@@*.:..............
 *        . :!!!!!!!!!!ooooo&@$*%%%*#@&*&&&&&&&*@@$&&&oooooooooooo.
 *        . :!!!!!!!!;;!;;:::@#;::.;@*         *@@o
 *                           @$    &@!.....  .*@@&................
 *          ................:@* .  ##.     .o#@%;
 *                        . &@%..:;@$:;!o&*$#*;  ..
 *                        . ;@@#$$$@#**&o!;:   ..
 *                           :;:: !@;        ..
 *                               ;@*........
 *                       ....   !@* ..
 *                 ......    .!%$! ..     | AUTHOR: dfer
 *         ......        .;o*%*!  .       | EMAIL: df_business@qq.com
 *                .:;;o&***o;.   .        | QQ: 3504725309
 *        .;;!o&****&&o;:.    ..          | WEBSITE: http://www.dfer.site
 * +----------------------------------------------------------------------
 *
 */

namespace plugins\dfer_thinkcmf_tools\controller;

use plugins\dfer_thinkcmf_tools\model\PluginMessageModel;

class ApiUserMessageBoardController extends BaseController
{
    /**
     * 留言提交
     * 
     * cmf_plugin_url('DferThinkcmfTools://ApiUserMessageBoard/addMessage')
     * 
     * http://{domain name}/plugin/dfer_thinkcmf_tools/api_user_message_board/addMessage
     */
    public function addMessage()
    {
        $model = new PluginMessageModel();

        $data = $this->request->post();
        $result = $this->validate($data, "Message");

        if ($result !== true) {
            $this->error($result);
        }
        $user = cmf_get_current_user();
        // var_dump($user);
        if ($this->request->isPost()) {

            $data['name'] = $user['user_nickname'];
            $data['phone'] = $user['mobile'];
            $data['create_time'] = time();

            //限制提交时间为一分钟一次
            if ($this->limitSendTimes()) {

                $data['msg'] = htmlspecialchars($data['msg']);
                $result = $model->allowField(['name', 'msg', 'phone', 'create_time'])->save($data);
                if ($result !== false) {
                    $this->success("您的留言成功，请耐心等待！", null, $user);
                } else {
                    $this->error("留言失败，请稍后再试！");
                }
            }
        }
    }

    //限制一分钟提交次数
    public function limitSendTimes()
    {
        session_start();
        $limit = 1;
        //限制的次数
        $ip = $this->getIp();
        $time = date('Y-m-d H:i');
        if (isset($_SESSION[$time])) {
            $ipnum = $this->reNum($_SESSION[$time], $ip);
        } else {
            $ipnum = 0;
        }
        // if ($ipnum >= $limit * 2) {
        //     exit("一分钟内只允许提交{$limit}次");
        // }

        if ($ipnum >= $limit) {
            $this->error("一分钟只能提交{$limit}次，您的请求过于频繁！");
        }

        if (isset($_REQUEST['url'])) {
            $url = $_REQUEST['url'];
        } else {
            $url = 0;
        }
        $_SESSION[$time][] = $ip;

        return true;
    }

    //获取ip
    private function getIp()
    {
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }


    //重置次数
    private function reNum($array, $get)
    {
        $n = 0;
        foreach ($array as $val) {
            if ($val == $get) {
                $n++;
            }
        }
        return $n;
    }
}
